package com.liu.file.table;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

//import org.apache.struts2.ServletActionContext;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

/**
 *  通用导出的工具类
 */
public class CommonOutput  extends PdfPageEventHelper {
	
	public static String EXPORTPATH;
	
	static {
//		EXPORTPATH = ServletActionContext.getServletContext().getRealPath("/")+"savefiles/";;
	}
	
	public  boolean outputPdf(Rectangle pSize,PdfPTable table,String fileName){
		
		Document doc = new Document(pSize,0,0,0,0);
		PdfWriter writer;
		try {
			writer = PdfWriter.getInstance(doc, new FileOutputStream(EXPORTPATH+fileName));
			writer.setPageEvent(this);
			doc.open();	
			doc.add(table);
			doc.close();
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (DocumentException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static void main(String[] args) throws DocumentException, Exception, Exception {
		Document doc = new Document(PageSize.A4,30,30,30,30);
		PdfWriter.getInstance(doc, new FileOutputStream("C:/Users/12961/Desktop/Velocity标签技术.pdf"));
		doc.open();	
		Image img = Image.getInstance("C:/Users/12961/Desktop/application.png");
		img.scalePercent(11f);
		Chunk c = new Chunk(img,0,0);
		PdfPTable t = new PdfPTable(1);
		PdfPCell cell = new PdfPCell(new Phrase(c	));
		t.addCell(cell);
		doc.add(t);
		doc.close();
	}
}
